//
//  IndexCollectionView.m
//  Mtime
//
//  Created by Mac on 15/8/1.
//  Copyright (c) 2015年 wxk. All rights reserved.
//

#import "IndexCollectionView.h"
#import "HomeModel.h"

@implementation IndexCollectionView

- (id)initWithFrame:(CGRect)frame {

    //1.设置flowlayout ——> 父类中完成
    
    //2.初始化self
    
    self = [super initWithFrame:frame];
    
    //3.设置代理 ——> 父类中完成
    
    if (self) {
        
        //4.注册单元格
        [self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"indexCell"];
        
        
        self.itemWidth = 100;
    }

    return self;
}

/*
 *实现data source方法
 *1.返回单元格的个数 ——> 父类中完成
 *2.返回单元格
 */

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {


    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"indexCell" forIndexPath:indexPath];
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:cell.contentView.bounds];
    
    HomeModel *model = self.dataList[indexPath.item];
    
    [imageV setImageWithURL:[NSURL URLWithString:model.img]];
    
    imageV.contentMode = UIViewContentModeScaleAspectFit;
    
    [cell.contentView addSubview:imageV];
    
    return cell;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    [collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    
    self.currentIndexPath = indexPath;
    
}

@end
